package com.h2.gwt.util.notification;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class Notification {
   
   public static final int PERMISSION_ALLOWED = 0;
   
   public static final int PERMISSION_NOT_ALLOWED = 1;
   
   public static final int PERMISSION_DENIED = 2;
   
   ////
   
   private JavaScriptObject _jsObject;
   
   ////
   
   public enum Permission {
      NONE,
      ALLOWED,
      NOT_ALLOWED,
      DENIED
   }
   
   ////
   ////
   
   public Notification() {
      //default constructor
   }
   
   public static Permission getPermission() {
      switch(checkPermissionImpl()){
         case PERMISSION_ALLOWED:
            return Permission.ALLOWED;
         case PERMISSION_NOT_ALLOWED:
            return Permission.NOT_ALLOWED;
         case PERMISSION_DENIED:
            return Permission.DENIED;
         default:
            return Permission.NONE; 
      }
   }
   
   public static native int checkPermissionImpl() /*-{
      return $wnd.webkitNotifications.checkPermission();
   }-*/;
   
   public static native void requestPermission(
         AsyncCallback<Void> callback) /*-{
      $wnd.webkitNotifications.requestPermission($entry(function() {
         callback.@com.google.gwt.user.client.rpc.AsyncCallback::onSuccess(Ljava/lang/Object;)(null);
      }));
   }-*/;
   
   private void callbackRequestPermission(AsyncCallback<Void> callback) {
      if (callback != null) {
         callback.onSuccess(null);
      }
   }
   
   private static native boolean detectNotificationSupport() /*-{
      return typeof $wnd.webkitNotifications != "undefined";
   }-*/;
   
   public void createNotification(String iconUrl, String title, String body) {
      _jsObject = null;
      _jsObject = this.createJSNotification(iconUrl, title, body);
   }
   
   public void createNotification(String contentUrl) {
      _jsObject = null;
      _jsObject = this.createHtmlNotification(contentUrl);
   }
   
   private native JavaScriptObject createJSNotification(
         String iconUrl, String title, String body) /*-{
      var notification = $wnd.webkitNotifications.createNotification(iconUrl, title, body);
      notification.ondisplay = function() { 
         this.@com.h2.gwt.util.notification.Notification::onDisplay()();
      };
      notification.onclose = function() { 
         this.@com.h2.gwt.util.notification.Notification::onClose()();
      };
      notification.onerror = function() { 
         this.@com.h2.gwt.util.notification.Notification::onError()();
      };
      return notification;
   }-*/;
   
   private native JavaScriptObject createHtmlNotification(String contentUrl) /*-{
      var notification = $wnd.webkitNotifications.createHTMLNotification(contentUrl);
      notification.ondisplay = function() { 
         this.@com.h2.gwt.util.notification.Notification::onDisplay()();
      };
      notification.onclose = function() { 
         this.@com.h2.gwt.util.notification.Notification::onClose()();
      };
      notification.onerror = function() { 
         this.@com.h2.gwt.util.notification.Notification::onError()();
      };
      return notification;
   }-*/;
   
   public native void show() /*-{
      var obj = this.@com.h2.gwt.util.notification.Notification::_jsObject;
      obj.show();
   }-*/;
   
   public void onDisplay() {
      //called from JS native function
   }
   
   public void onClose() {
      //called from JS native function
   }

   public void onError() {
      //called from JS native function
   }
   
   public static boolean isSupported() {
      return detectNotificationSupport();
   }
}
